package com.ysyc.driver_ee.api;


import com.google.gson.Gson;
import com.google.gson.TypeAdapter;

import com.google.gson.stream.JsonReader;
import com.ysyc.driver_ee.base.BaseBean;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import okhttp3.ResponseBody;
import retrofit2.Converter;

public final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

    private final TypeAdapter<T> adapter;
    private final Gson gson;

    GsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        InputStream inputStream = value.byteStream();
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = inputStream.read(b)) != -1; ) {
            out.append(new String(b, 0, n));
        }
        String string = out.toString();
        Gson gson = new Gson();
        BaseBean baseBean = gson.fromJson(string, BaseBean.class);
        String s = gson.toJson(baseBean.data);

        if (baseBean.result.code == 0 && "0".equals(baseBean.result.status)) {
            if (null == baseBean.data) {
                return (T) "";
            }
            JsonReader jsonReader = gson.newJsonReader(new StringReader(s));
            T read = adapter.read(jsonReader);
            return read;
        } else {
            return (T) new ApiException(baseBean.result.msg, baseBean.result.code);
        }



//        BaseBean<T> baseBean = JSON.parseObject(string, new TypeReference<BaseBean<T>>() {});
//        if (null==baseBean.data){
//            return (T)"";
//        }
//        return baseBean.data;

//        JsonReader jsonReader = gson.newJsonReader(new StringReader(string));
//        BaseBean<T> t = gson.fromJson(string,BaseBean.class);
//        Gson gson = new Gson();
//        BaseBean<T> baseBean = gson.fromJson(string, new TypeToken<BaseBean<T>>() {}.getType());


//        return t;

//        Gson gson = new Gson();
//        BaseBean<T> baseBean = gson.fromJson(string,BaseBean.class);
//        if (baseBean.result.code == 0 && "0".equals(baseBean.result.status)) {
//            if (null == baseBean.data) {
//                return (T) "";
//            }
//            JsonReader jsonReader = gson.newJsonReader(new StringReader(string));
//            T t = adapter.read(jsonReader);
//            return t;
//        } else {
//            return (T) new ApiException(baseBean.result.msg, baseBean.result.code);
//        }




//        BaseBean baseBean = (BaseBean) t;


//        return t;

//        try {
//
//            return t;
//            if (baseBean.result.code==0&&"0".equals(baseBean.result.status)) {
//                if (null==baseBean.data){
//                    return (T)"";
//                }
//                return baseBean.data;
//            } else {
//                return (T) new ApiException(baseBean.result.msg,baseBean.result.code);
//            }
//        } catch (Exception e) {
//            return (T) string;
//        } finally {
//            value.close();
//        }
    }



}
